package com.yilin.tms.user.controller;

import com.yilin.commons.util.StringUtil;
import com.yilin.tms.core.application.PageData;
import com.yilin.tms.core.application.ReturnData;
import com.yilin.tms.core.commons.annotation.SkipLoginVerify;
import com.yilin.tms.core.commons.base.BaseController;
import com.yilin.tms.core.commons.constant.HttpConstant;
import com.yilin.tms.core.commons.utils.ParamUtil;
import com.yilin.tms.core.commons.utils.UserUtil;
import com.yilin.tms.core.entity.system.system.Jurisdiction;
import com.yilin.tms.core.entity.truck.TruckVehicle;
import com.yilin.tms.core.entity.user.account.Account;
import com.yilin.tms.core.entity.user.account.AdvanceMoney;
import com.yilin.tms.core.entity.user.account.Authority;
import com.yilin.tms.core.remote.ISystemRemote;
import com.yilin.tms.core.remote.IVehicleRemote;
import com.yilin.tms.user.service.impl.AccountService;
import com.yilin.tms.user.service.impl.AdvanceMoneyService;
import com.yilin.tms.user.service.impl.AuthorityService;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@RestController
@RequestMapping("/user/authority/")
public class AuthorityController extends BaseController {
    @Resource
    private AuthorityService authorityService;
    @Resource
    private AccountService accountService;
    @Resource
    private ISystemRemote systemRemote;

    @PostMapping("updateAuthority")
    @SkipLoginVerify
    public ReturnData<Object> updateAuthority(String accountId, String[] jurisdictionIds) {
        ParamUtil.isNull(accountId, "请传入ID参数");
        Account account = accountService.getAccountById(accountId);
        ParamUtil.isNull(account, "获取用户失败");
        List<Jurisdiction> jurisdictionList = ParamUtil.isSuccess(systemRemote.getJurisdictionListByIds(String.join(",",jurisdictionIds)));
        authorityService.saveAuthority(account, jurisdictionList);
        UserUtil.removeLoginUser(account.getToken());
        return jsonView(true, "保存成功");
    }

    @PostMapping("getAuthorityList")
    @SkipLoginVerify
    public ReturnData<Object> getAuthorityList(String accountId) {
        ParamUtil.isNull(accountId, "请传入ID参数");
        List<Authority> authorityList = authorityService.getAuthorityListByyAccountId(accountId);
        return jsonView(true, "保存成功", authorityList);
    }


}
